package com.lovelyj.trip_by_train.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lovelyj.trip_by_train.common.R;
import com.lovelyj.trip_by_train.entity.Administrator;
import com.lovelyj.trip_by_train.entity.User;
import com.lovelyj.trip_by_train.service.AdministratorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/administrator")
public class AdministratorController{

    @Autowired
    private AdministratorService administratorService;

    @PostMapping("/login")
    public R<String> login(HttpServletRequest request, @RequestBody Administrator administrator){
        String username = administrator.getUsername();
        String password = administrator.getPassword();
        LambdaQueryWrapper<Administrator> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Administrator::getUsername, username);

        password= DigestUtils.md5DigestAsHex(password.getBytes());
        queryWrapper.eq(Administrator::getPassword,password);
        Administrator admin = administratorService.getOne(queryWrapper);
        if(admin!=null){
            request.getSession().setAttribute("administrator",admin.getId());
            return R.success("登录成功");
        }
        return R.error("账号或密码错误");
    }


    @PostMapping("/selectAll")
    public R<List<Administrator>> selectAll(HttpServletRequest request){
        log.info("selectAll...");
        List<Administrator> admin=administratorService.list();
        return R.success(admin);
    }

    @GetMapping("/page")
    public R<Page<Administrator>> selectPage(HttpServletRequest request,Integer page,String username,String phone){
        log.info("selectPage..."+page);
        Page<Administrator> pageInfo=new Page<>(page,10);

        LambdaQueryWrapper<Administrator> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(username!=null,Administrator::getUsername,username);
        queryWrapper.like(phone!=null,Administrator::getPhone,phone);
        administratorService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @PutMapping("/register")
    public R<String> register(HttpServletRequest request, @RequestBody Administrator administrator){
        log.info("注册中");
        LambdaQueryWrapper<Administrator> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Administrator::getUsername,administrator.getUsername());
        Administrator admin=administratorService.getOne(queryWrapper);
        if(admin!=null){return R.error("用户名已存在");}
        String password=administrator.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        administrator.setPassword(password);
        administratorService.save(administrator);
        return R.success("注册成功");

    }


    @PostMapping("/deleteByIds")
    public R<String> deleteByIds(HttpServletRequest request,@RequestBody List<Long> selectedIds){

        log.info("deleteByIds...");
        administratorService.removeByIds(selectedIds);

        return R.success("删除成功");
    }

    @DeleteMapping
    public R<String> deleteById(HttpServletRequest request,Long id){

        log.info("deleteById..."+id);
        administratorService.removeById(id);
        return R.success("删除成功");
    }

    @PutMapping("/update")
    public R<String> update(HttpServletRequest request,@RequestBody Administrator administrator){
        log.info("update..."+administrator.toString());
        if(administrator.getPassword()!=null){
            String password = administrator.getPassword();
            password = DigestUtils.md5DigestAsHex(password.getBytes());
            administrator.setPassword(password);
        }
        administratorService.updateById(administrator);
        return R.success("修改成功");
    }

}
